
#include "ASettings.h"
#include "AString.h"
#include "AXML.h"

CASettings::CASettings()
{
}

CASettings::~CASettings()
{
}

void CASettings::initialize(CAXMLNode* pnodeSettings)
{
	GUARD_FUNCTION();

	_Assert(null != pnodeSettings);
	
	CAXMLNodes* pnodes = pnodeSettings->nodes("key");
	CCObject* pkey;
	CCARRAY_FOREACH(pnodes, pkey)
	{
		CAXMLNode* pnode = (CAXMLNode*)pkey;
		_Assert(pnode->hasAttr("name"));
		CAString name = pnode->attr("name");

		CAValueObject* pv = new CAValueObject();

		if (pnode->hasAttr("float"))
		{
			float v;
			_Verify(CAStringUtils::toFloat(pnode->attr("float"), v));
			pv->setFloat(v);
		}
		else if (pnode->hasAttr("CAString"))
		{
			pv->setString(pnode->attr("CAString"));
		}
		else if (pnode->hasAttr("point"))
		{
			CCPoint v;
			_Verify(CAStringUtils::toPoint(pnode->attr("point"), v));
			pv->setPoint(v);
		}
		else if (pnode->hasAttr("size"))
		{
			CCSize v;
			_Verify(CAStringUtils::toSize(pnode->attr("size"), v));
			pv->setSize(v);
		}
		else if (pnode->hasAttr("rect"))
		{
			CCRect v;
			_Verify(CAStringUtils::toRect(pnode->attr("rect"), v));
			pv->setRect(v);
		}
		else if (pnode->hasAttr("bool"))
		{
			bool v;
			_Verify(CAStringUtils::toBool(pnode->attr("bool"), v));
			pv->setBool(v);
		}
		else if (pnode->hasAttr("int"))
		{
			int v;
			_Verify(CAStringUtils::toInt(pnode->attr("int"), v));
			pv->setInteger(v);
		}
		else
		{
			_Assert(false);
		}
		this->setObject(pv, name);
	}
}
